package com.infore.airsupervise.biz.impl;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.MyApplication;
import com.infore.airsupervise.bean.AirStationE;
import com.infore.airsupervise.bean.PointAnalyzeE;
import com.infore.airsupervise.bean.StationDetailE;
import com.infore.airsupervise.biz.StationDetailBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * @features: 和站点信息相关网络请求业务接口实现
 * @author: WuTao
 * @date: 2017/3/30 10:14
 */
public class StationDetailBizImpl implements StationDetailBiz {
    @Override
    public Call<BaseResponse> sendStationDetailRequest(String mn, final OnServerResponseListener<StationDetailE> listener) {
        Map<String , Object> params = new HashMap<>();
        params.put("mn",mn);
        Call<BaseResponse>  call = HttpUtil.getHttpService().postParams(HttpAPI.GET_STATION_DETAIL,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                StationDetailE detailE = JSONUtil.parseBean(response.getData(), Constants.RESPONSE_BEAN_KEY,StationDetailE.class);
                listener.onSucceed(detailE,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String titleRes, String descRes) {
                super.onException(errorCode, titleRes, descRes);
                listener.onFailed(errorCode,titleRes, descRes);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendHistoryPictureRequest(String mn, String time, final OnServerResponseListener<List<StationDetailE.SitePicture>> listener) {
        Map<String , Object>  params = new HashMap<>();
        params.put("mn" ,mn);
        params.put("time" , time);
        Call call = HttpUtil.getHttpService().postParams(HttpAPI.GET_STATION_PICTURE,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<StationDetailE.SitePicture> pictures = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY, StationDetailE.SitePicture.class);
                listener.onSucceed(pictures,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String titleRes, String descRes) {
                super.onException(errorCode, titleRes, descRes);
                listener.onFailed(errorCode,titleRes, descRes);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendRecentlyDataRequest(String mn, String startTime , String endTime,  final OnServerResponseListener<List<PointAnalyzeE>> listener) {
        Map<String , Object>  params = new HashMap<>();
        params.put("mn" ,mn);
        params.put("startTime" ,startTime);
        params.put("endTime" ,endTime);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_RECENTLY,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<PointAnalyzeE> list = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY, PointAnalyzeE.class);
                listener.onSucceed(list,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String titleRes, String descRes) {
                super.onException(errorCode, titleRes, descRes);
                listener.onFailed(errorCode,titleRes, descRes);
            }
        });
        return null;
    }
}
